package com.hyacinth.cloudnote.common.dto.result;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 分页数据返回结果
 */
@NoArgsConstructor
@Getter
@Setter
public class PageResultDTO<T> extends ListResultDTO<T> {
    private static final long serialVersionUID = -6221823151976151389L;
    /**
     * 分页信息
     */
    @ApiModelProperty(value = "分页信息", position = 11)
    @JsonProperty(index = 11)
    protected PageDataDTO pageable;

    protected PageResultDTO(final HttpStatus status) {
        super(status);
    }

    ///////////////////////////////////////
    // Builder
    ///////////////////////////////////////
    public static <T> PageResultDTO<T> success(final Page<T> pageData) {
        Assert.notNull(pageData, "The formal parameter 'pageData' cannot be null.");

        final PageResultDTO<T> result = new PageResultDTO<>(HttpStatus.OK);
        result.setData(pageData.getContent());
        result.setPageable(PageDataDTO.valueOf(pageData));
        return result;
    }

    public static <T> PageResultDTO<T> success(final List<T> listData, final Page<?> pageData) {
        Assert.notNull(pageData, "The formal parameter 'pageData' cannot be null.");

        final PageResultDTO<T> result = new PageResultDTO<>(HttpStatus.OK);
        result.setData(listData);
        result.setPageable(PageDataDTO.valueOf(pageData));
        return result;
    }
}
